## export LD_LIBRARY_PATH=/usr/local/BerkeleyDB.4.7/lib
SRC=src
BUILD=build
BIN=bin
CC=gcc
DBVERSION=4.6
CCFLAG=-isystem /usr/local/BerkeleyDB.$(DBVERSION)/include -I src -Wall `xml2-config --cflags `
LDFLAG=`xml2-config --libs ` -L/usr/local/BerkeleyDB.$(DBVERSION)/lib -ldb
JAVAC=${JAVA_HOME}/bin/javac
JAR=${JAVA_HOME}/bin/jar
JAVA=${JAVA_HOME}/bin/java
VELOCITYROOT=/usr/local/package/velocity-1.5
CLASSPATH=$(SRC)/java:$(VELOCITYROOT)/velocity-dep-1.5.jar
OPERONLIB=$(BUILD)/morestdlib.o \
		$(BUILD)/morestring.o \
		$(BUILD)/xml.o \
		$(BUILD)/morestdio.o \
		$(BUILD)/moredb.o \
		$(BUILD)/vector.o \
		$(BUILD)/scalar.o \
		$(BUILD)/ynode.o \
		$(BUILD)/algorithm.o \
		$(BUILD)/debug.o \
		$(BUILD)/fid.o \
		$(BUILD)/organisms.o \
		$(BUILD)/chromosomes.o 
all:operon
operon:$(BIN)/operon
tests:$(BIN)/test1
$(BIN)/test1:$(SRC)/test/test01.c \
		$(OPERONLIB)
	$(CC) $(CCFLAG) -o $@ \
		$(SRC)/test/test01.c \
		$(OPERONLIB)  $(LDFLAG)
	$@

test2:$(BIN)/test2
	$(BIN)/$@
$(BIN)/test2:$(SRC)/test/test02.c 
	$(CC) $(CCFLAG) -o $@ \
		$(SRC)/test/test02.c \
		$(LDFLAG)


test3:$(BIN)/test3
$(BIN)/test3:$(SRC)/test/test03.c $(SRC)/core/snpIds.c $(OPERONLIB) scanners
	$(CC) $(CCFLAG) -o $@ \
		$(SRC)/test/test03.c \
		$(SRC)/core/snpIds.c \
		$(BUILD)/*.tab.o \
		$(BUILD)/*_lexer.o \
		$(OPERONLIB)  $(LDFLAG)
	$@


test4:$(BIN)/test4
	$(BIN)/test4
$(BIN)/test4:$(SRC)/core/snp127_dump.c $(SRC)/core/snpIds.c $(OPERONLIB) scanners
	$(CC) $(CCFLAG) -o $@ \
		$(SRC)/core/snp127_dump.c \
		$(SRC)/core/snpIds.c \
		$(BUILD)/*.tab.o \
		$(BUILD)/*_lexer.o \
		$(OPERONLIB)  $(LDFLAG)

$(BIN)/test5:$(SRC)/core/SnpMap128_dump.c $(SRC)/core/SnpMap.c $(OPERONLIB) scanners
	$(CC) $(CCFLAG) -o $@ \
		$(SRC)/core/SnpMap128_dump.c \
		$(SRC)/core/SnpMap.c \
		$(BUILD)/*.tab.o \
		$(BUILD)/*_lexer.o \
		$(OPERONLIB)  $(LDFLAG)


$(BIN)/operon:$(SRC)/core/operon.c $(OPERONLIB)
	$(CC) $(CCFLAG) -DTEST_THIS_CODE -o $@ $(SRC)/core/operon.c $(OPERONLIB) $(LDFLAG)
	 $@
 
scanners:generatorcode $(OPERONLIB)
	(cd $(SRC)/core; make -f Makefile )

generatorcode:$(BUILD)/codegenerator.jar
	$(JAVA) -cp $(CLASSPATH):$(BUILD)/codegenerator.jar fr.cephb.lindenb.operon.CodeGenerator \
		-d $(SRC) \
		config/operon-structures.xml
$(BUILD)/fid.o: $(SRC)/core/fid.c $(SRC)/core/fid.h $(SRC)/core/chromosomes.h $(SRC)/core/chromosomes.c
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/core/fid.c		
$(BUILD)/vector.o: $(SRC)/util/vector.c $(SRC)/util/vector.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/util/vector.c
$(BUILD)/morestdlib.o: $(SRC)/lang/morestdlib.c $(SRC)/lang/morestdlib.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/lang/morestdlib.c
$(BUILD)/morestring.o: $(SRC)/lang/morestring.c $(SRC)/lang/morestring.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/lang/morestring.c
$(BUILD)/morestdio.o: $(SRC)/lang/morestdio.c $(SRC)/lang/morestdio.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/lang/morestdio.c
$(BUILD)/morexml.o: $(SRC)/xml/morexml.c $(SRC)/xml/morexml.h $(SRC)/xml/xml.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/xml/morexml.c
$(BUILD)/xml.o: $(SRC)/xml/xml.c $(SRC)/xml/xml.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/xml/xml.c
$(BUILD)/debug.o: $(SRC)/util/debug.c $(SRC)/util/debug.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/util/debug.c
$(BUILD)/algorithm.o: $(SRC)/util/algorithm.c $(SRC)/util/algorithm.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/util/algorithm.c
$(BUILD)/moredb.o: $(SRC)/berkeley/moredb.c $(SRC)/berkeley/moredb.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/berkeley/moredb.c
$(BUILD)/scalar.o: $(SRC)/lexyacc/scalar.c $(SRC)/lexyacc/scalar.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/lexyacc/scalar.c
$(BUILD)/ynode.o: $(SRC)/lexyacc/ynode.c $(SRC)/lexyacc/ynode.h
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/lexyacc/ynode.c
$(BUILD)/organisms.o:$(SRC)/core/organisms.h $(SRC)/core/organisms.c
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/core/organisms.c
$(BUILD)/chromosomes.o:$(SRC)/core/chromosomes.h $(SRC)/core/chromosomes.c
	$(CC) $(CCFLAG) -o $@ -c $(SRC)/core/chromosomes.c

$(BUILD)/codegenerator.jar:$(SRC)/java/fr/cephb/lindenb/operon/CodeGenerator.java \
				$(SRC)/velocity/XXXstruct_select.c.vm  \
				$(SRC)/velocity/yacc.vm  \
				$(SRC)/velocity/XXXstruct2mysql.c.vm  \
				$(SRC)/velocity/XXXstruct.h.vm  \
				$(SRC)/velocity/Makefile.vm  \
				$(SRC)/velocity/XXXstruct.c.vm 
	$(JAVAC) -cp $(CLASSPATH) -sourcepath $(SRC)/java $(SRC)/java/fr/cephb/lindenb/operon/CodeGenerator.java 
	$(JAR) cf $@ -C $(SRC)/java fr -C $(SRC) velocity

touch:
	find ./ -name "*.c" -o -name "*.h" -o -name "*.java" -exec touch '{}' ';'
	find $(BUILD) -type f -exec touch '{}' ';'

